var reports_ids;

function reports_refresh() {
  // save old IDs to enable flashing
  reports_old_ids = reports_get_ids();
  
  // run the ajax request
  new Ajax.Updater($('index_table'), "/reports?auto=1", {
    method: 'get',
    onComplete: reports_flash_new
  });
}


// gets IDs of each row in index table
function reports_get_ids() {
  var ids = [];
  if ($('index_table_body')) {
    var rows = $('index_table_body').getElementsByTagName('tr');
    for (var i = 0; i < rows.length; i++) ids.push(rows[i].id);
  }
  return ids;
}

function reports_flash_new(transport) {
  var new_ids = reports_get_ids();
  for (var i = 0; i < new_ids.length; i++) {
    if (reports_old_ids.indexOf(new_ids[i]) == -1)
      setTimeout(function(id){new Effect.Highlight(id);}.curry(new_ids[i]), 100);
  }
}

function report_change_importance(link, report_id) {
  // get current importance
  var current = link.innerHTML;
  
  // compute new importance
  var updated;
  switch (current) {
    case "?": updated = "yes"; break;
    case "yes": case "auto": updated = "no"; break;
    case "no": default: updated = "?";
  }
  
  // write new importance to html
  link.innerHTML = updated;
  
  // send to server
  new Ajax.Request("reports/" + report_id + "/change_importance", {method: 'post', parameters: {updated: updated}});
}